package com.lcu.wl.modules.sys.service.impl;

import com.lcu.wl.common.utils.MapUtils;
import com.lcu.wl.modules.sys.entity.Role;
import com.lcu.wl.modules.sys.entity.RoleMenu;
import com.lcu.wl.modules.sys.mapper.RoleMenuMapper;
import com.lcu.wl.modules.sys.service.IRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 角色与菜单对应关系 服务实现类
 * </p>
 *
 * @author wanglei
 * @since 2021-01-15
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRoleMenu(Role resources) {
        Long roleId  = resources.getRoleId();

        //删除角色菜单关系
        this.removeByMap(new MapUtils().put("role_id", roleId));

        List<Long> menuIds = resources.getMenuIds();
        if(menuIds != null){
            for (Long menuId : menuIds){
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                //保存角色菜单关系
                this.save(roleMenu);
            }
        }
    }

    @Override
    public List<Long> getMenuIds(Long roleId) {
        return baseMapper.selectMenuIdByRoleId(roleId);
    }

    @Override
    public void delByRoleIdsBatch(Long[] roleIds) {
        baseMapper.delByRoleIdsBatch(roleIds);
    }
}
